package top.soulblack.purchasesale.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import top.soulblack.purchasesale.model.Goods;
import top.soulblack.purchasesale.model.StoreHouse;
import top.soulblack.purchasesale.model.Supplier;
import top.soulblack.purchasesale.model.VO.GoodsToIdVO;
import top.soulblack.purchasesale.service.GoodsService;
import top.soulblack.purchasesale.service.StoreHouseService;
import top.soulblack.purchasesale.service.SupplierService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * create by 小川
 */

@Controller
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private StoreHouseService storeHouseService;

    @RequestMapping("/list")
    public String List(Model model, HttpServletRequest request){

        List<GoodsToIdVO> good =  goodsService.selectToId();
        List<GoodsToIdVO> goods = null;
        List<StoreHouse> storeHouses = storeHouseService.findAll();
        List<Supplier> suppliers = supplierService.selectAll();
        if (request.getParameter("goodsName") != "" && request.getParameter("goodsName") != null) {
            String goodsName = request.getParameter("goodsName");
            model.addAttribute("goodsName",goodsName);
            goods = goodsService.selectClearFromName(goodsName);
        }
        model.addAttribute("supplier",suppliers);
        model.addAttribute("storeHouse",storeHouses);
        model.addAttribute("goods",goods);
        model.addAttribute("good",good);
        return "purchase/list";
    }

    @RequestMapping("/return")
    public String Return(Model model, HttpServletRequest request){

        List<Supplier> suppliers =supplierService.selectAll();
        List<StoreHouse> storeHouses=storeHouseService.findAll();
        List<GoodsToIdVO> good =  goodsService.selectToId();
        List<GoodsToIdVO> goods = null;
        if (request.getParameter("goodsName") != "" && request.getParameter("goodsName") != null) {
            String goodsName = request.getParameter("goodsName");
            model.addAttribute("goodsName",goodsName);
            goods = goodsService.selectClearFromName(goodsName);
        }
        model.addAttribute("supplier",suppliers);
        model.addAttribute("storeHouses",storeHouses);
        model.addAttribute("goods",goods);
        model.addAttribute("good",good);
        return "/purchase/return";
    }

    @RequestMapping("/supplier")
    public String Supplier(Model model, HttpServletRequest request){

        List<Supplier> suppliers = supplierService.selectAll();
        if (request.getParameter("name") != "" && request.getParameter("name") != null){
            String supplierName = request.getParameter("name");
            List<Supplier> supplier = supplierService.selectForName(supplierName);
            model.addAttribute("supplier",supplier);
        }
        model.addAttribute("suppliers",suppliers);
        return "purchase/supplier";
    }
}
